package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.Manifest;
import android.os.Bundle;

import com.example.myapplication.fragment.FragmentAdapter;
import com.example.myapplication.fragment.WordFragment;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallback;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.callback.RequestCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;

import java.security.acl.Permission;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPager2 vp = findViewById(R.id.vp);

        FragmentAdapter adapter = new FragmentAdapter(this);
        adapter.addFragment(new WordFragment());

        vp.setAdapter(adapter);

        initPermission();
    }

    private void initPermission() {
        PermissionX.init(this)
                .permissions(
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .onExplainRequestReason(new ExplainReasonCallback() {
                    @Override
                    public void onExplainReason(@NonNull ExplainScope scope,
                                                @NonNull List<String> deniedList)
                    {   scope.showRequestReasonDialog(deniedList,
                                "用于保存朗读单词的音频文件",
                                "确定",
                                "取消");}
                }).onForwardToSettings(new ForwardToSettingsCallback() {
                    @Override
                    public void onForwardToSettings(@NonNull ForwardScope scope,
                                                    @NonNull List<String> deniedList) {
                        scope.showForwardToSettingsDialog(deniedList,
                                "跳转到设置界面",
                                "确定");
                    }
                }).request(new RequestCallback() {
                    @Override
                    public void onResult(boolean allGranted,
                                         @NonNull List<String> grantedList,
                                         @NonNull List<String> deniedList) {
                        if( !allGranted) {
                            finish();
                        }

                    }
                });
    }
}